package com.cnswhy.cloud.common.doc.gateway.configure;

import com.cnswhy.cloud.common.doc.gateway.filter.CnswhyDocGatewayHeaderFilter;
import com.cnswhy.cloud.common.doc.gateway.handler.CnswhyDocGatewayHandler;
import com.cnswhy.cloud.common.doc.gateway.properties.CnswhyDocGatewayProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger.web.UiConfiguration;

/**
 * @author MrBird
 */
@Configuration
@EnableConfigurationProperties(CnswhyDocGatewayProperties.class)
@ConditionalOnProperty(value = "cnswhy.doc.gateway.enable", havingValue = "true", matchIfMissing = true)
public class CnswhyDocGatewayAutoConfigure {

    private final CnswhyDocGatewayProperties cnswhyDocGatewayProperties;
    private SecurityConfiguration securityConfiguration;
    private UiConfiguration uiConfiguration;

    public CnswhyDocGatewayAutoConfigure(CnswhyDocGatewayProperties febsDocGatewayProperties) {
        this.cnswhyDocGatewayProperties = febsDocGatewayProperties;
    }

    @Autowired(required = false)
    public void setSecurityConfiguration(SecurityConfiguration securityConfiguration) {
        this.securityConfiguration = securityConfiguration;
    }

    @Autowired(required = false)
    public void setUiConfiguration(UiConfiguration uiConfiguration) {
        this.uiConfiguration = uiConfiguration;
    }

    @Bean
    public CnswhyDocGatewayResourceConfigure febsDocGatewayResourceConfigure(RouteLocator routeLocator, GatewayProperties gatewayProperties) {
        return new CnswhyDocGatewayResourceConfigure(routeLocator, gatewayProperties);
    }

    @Bean
    public CnswhyDocGatewayHeaderFilter febsDocGatewayHeaderFilter() {
        return new CnswhyDocGatewayHeaderFilter();
    }

    @Bean
    public CnswhyDocGatewayHandler febsDocGatewayHandler(SwaggerResourcesProvider swaggerResources) {
        CnswhyDocGatewayHandler cnswhyDocGatewayHandler = new CnswhyDocGatewayHandler();
        cnswhyDocGatewayHandler.setSecurityConfiguration(securityConfiguration);
        cnswhyDocGatewayHandler.setUiConfiguration(uiConfiguration);
        cnswhyDocGatewayHandler.setSwaggerResources(swaggerResources);
        cnswhyDocGatewayHandler.setProperties(cnswhyDocGatewayProperties);
        return cnswhyDocGatewayHandler;
    }
}
